$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/databaseinfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 20, key: true},
            {label: '数据源名称', name: 'connname', index: 'connname', width: 50},
            {label: '数据库名称', name: 'dbname', index: 'dbname', width: 50},
            {label: '数据库驱动', name: 'drivername', index: 'drivername', width: 80},
            {label: '数据库地址', name: 'url', index: 'url', width: 170},
            {label: '用户名', name: 'username', index: 'username', width: 40},
            {label: '密码', name: 'password', index: 'password', width: 40},
            {label: '数据库类型', name: 'datasourcetype', index: 'datasourcetype', width: 40}

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {

        showList: true,
        title: null,
        databaseinfo: {
            datasourcetype:""
        },
        dbArr: [
            {
                id: 'mysql',
                name: 'mysql'
            },
            {
                id: 'postgresql',
                name: 'postgresql'
            },
            {
                id: 'oracle',
                name: 'oracle'
            },
            {
                id: 'sqlserver',
                name: 'sqlserver'
            }
        ]
    },
    created: function () {
        // 在组件创建之后，把默认选中项的value值赋给绑定的属性
        //如果没有这句代码，select中初始化会是空白的，默认选中就无法实现
        this.databaseinfo.datasourcetype = this.dbArr[0].id;
    },

    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.databaseinfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },

        create_tableInfo: function (event) {

                var that = this;
                var id = getSelectedRow();
                if (id == null) {
                    return;
                }
                var layerId = layer.msg('生成结果中，请等待！', {time: 0});
                var grid = $("#jqGrid");
                var selectedIDs = grid.getGridParam("selarrrow");
                var rowData = grid.jqGrid('getRowData', id);
                console.log(rowData, "+++++++++______________");


                var connid = rowData.id;

                var url = "sys/databaseinfo/createTableInfo";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    //contentType: "application/json",
                    // data: JSON.stringify({connid: connid}),
                    data: {
                        connid: connid
                    },

                    success: function (r) {
                        if (r.code === 0) {
                            console.log(r);

                            layer.close(layerId);
                        } else {
                            layer.alert("查询失败！");
                            layer.close(layerId);
                        }
                        return false;
                    }
                });

        },

        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.databaseinfo.id == null ? "sys/databaseinfo/save" : "sys/databaseinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.databaseinfo),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        databaseTest: function (event) {
            $('#btnTest').button('loading').delay(1000).queue(function () {
                var url = "sys/databaseinfo/checkDatabase";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.databaseinfo),
                    success: function (r) {
                        if (r.code === 0) {
                            //layer.msg("操作成功", {icon: 1});
                            layer.alert(r.msg);
                            //vm.reload();
                            $('#btnTest').button('reset');
                            $('#btnTest').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnTest').button('reset');
                            $('#btnTest').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/databaseinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            // alert(1111);
            $.get(baseURL + "sys/databaseinfo/info/" + id, function (r) {


                vm.databaseinfo = r.databaseinfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});